package com.youchuang.action.main;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.youchuang.action.PageAction;
import com.youchuang.bean.Book;
import com.youchuang.bean.Category;
import com.youchuang.common.Constant;
import com.youchuang.service.MainService;

import net.sf.json.JSONArray;

/**
 * 点击主页左栏目录后，所进入页面的左栏目录
 * 
 * @author Administrator
 *
 */
@Namespace(value = "/main")
@ParentPackage("shopping-default")
public class BookListAction extends PageAction {
	private Logger logger = Logger.getLogger(BookListAction.class.getSimpleName());
	private String c1;
	private String c2;

	private int totalPnum;
	private List<Category> cats;
	private List<Book> books;

	@Resource
	private MainService mainService;

	@Action(value = "booklist", results = {
			@Result(name = Constant.LIST, location = "/WEB-INF/jsp/main/book_list.jsp") })
	public String booklist() {
		logger.info("booklist is execute");
		cats = mainService.findCategoryByParentId(c1);
		int begin = getBeginIndex();
		books = mainService.findBookLimmitByCategoryId(c2, begin, size);
		logger.info("response json books = " + JSONArray.fromObject(books).toString());
		return Constant.LIST;
	}

	@Action(value = "booklisthot", results = {
			@Result(name = Constant.LIST, location = "/WEB-INF/jsp/main/book_list.jsp") })
	public String booklisthot() throws Exception {
		logger.info("booklisthot is execute");
		cats = mainService.findCategoryByParentId(c1);

		int begin = getBeginIndex();
		books = mainService.findBookLimmitByCategoryIdHot(c2, begin, size);

		logger.info("response json books = " + JSONArray.fromObject(books).toString());
		return Constant.LIST;
	}

	@Action(value = "booklistnew", results = {
			@Result(name = Constant.LIST, location = "/WEB-INF/jsp/main/book_list.jsp") })
	public String booklistnew() throws Exception {
		logger.info("booklistnew is execute");
		cats = mainService.findCategoryByParentId(c1);

		int begin = getBeginIndex();
		books = mainService.findBookLimmitByCategoryIdNew(c2, begin, size);

		logger.info("response json books = " + JSONArray.fromObject(books).toString());
		return Constant.LIST;
	}

	private int getBeginIndex() {
		int pnum = mainService.findCategoryProductCountByCategoryId(c2);
		// TODO:多线程出现的问题
		if (pnum == 0) {
			maxPage = 1;
		} else if (pnum % size == 0) {
			maxPage = pnum / size;
		} else {
			maxPage = pnum / size + 1;
		}
		int begin = (page - 1) * size;
		return begin;
	}

	public String getC1() {
		return c1;
	}

	public void setC1(String c1) {
		this.c1 = c1;
	}

	public String getC2() {
		return c2;
	}

	public void setC2(String c2) {
		this.c2 = c2;
	}

	public int getTotalPnum() {
		return totalPnum;
	}

	public void setTotalPnum(int totalPnum) {
		this.totalPnum = totalPnum;
	}

	public List<Category> getCats() {
		return cats;
	}

	public void setCats(List<Category> cats) {
		this.cats = cats;
	}

	public List<Book> getBooks() {
		return books;
	}

	public void setBooks(List<Book> books) {
		this.books = books;
	}

}
